///
/// 张诚宇
///
/// 我的页面
///
/// 2022.6.14
///
///

import 'dart:convert';

import 'package:flutter/material.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:zhangchengyu20220614/model/LoginEntity.dart';
import 'package:zhangchengyu20220614/view/login.dart';

class MyPage extends StatefulWidget {
  const MyPage({Key key}) : super(key: key);

  @override
  _MyPageState createState() => _MyPageState();
}

class _MyPageState extends State<MyPage> {

  Result _result;

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    getSpData();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(

      ),
      body: Container(
        width: MediaQuery.of(context).size.width,
        child: Column(
          children: [
            _result == null ? RaisedButton(onPressed: (){
              Navigator.push(context, MaterialPageRoute(builder: (context)=>LoginPage())).then((value) {
                // Fluttertoast.showToast(msg: 'a');
                if(value!=null && value){
                  getSpData();
                }
              });
            },child: Text('登录',style: TextStyle(color: Colors.deepOrange),),color: Colors.white,shape: RoundedRectangleBorder(
              borderRadius: BorderRadius.circular(20)
            ),) : ClipRRect(
              borderRadius: BorderRadius.circular(25),
              child: Image.network(_result.headPic,height: 50,width: 50,),
            )
          ],
        ),
      ),
    );
  }

  void getSpData() async{
    var sharedPreferences = await SharedPreferences.getInstance();
    var string = sharedPreferences.getString('userMsg');
    print(string);
    var json = jsonDecode(string);
    setState(() {
      _result=Result.fromJson(json);
    });
  }
}
